﻿var GuideValidationClientSideHide = false;
var PageNumber = 1;
var CurrentTab = 1;
var CurrentTabElement = null;
var IsSearchView = false;
var GuideIDView = 0;
var SelectedLCountryID = null;
var Phone = null;
var Fax = null;
var EmrgcyContactPhone = null;
var GuideAutoComElem = null;
var GuideAutoComElemVal = null;
var GuideTimerId = null;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("ManageGuides") != -1) {
        $.validator.addMethod("ManageGuideRequireds", $.validator.methods.required, Messages["ManageGuideRequireds"]);
        $.validator.addMethod("ManageGuideDate", $.validator.methods.datePicker, Messages["ManageGuideDate"]);
        $.validator.addMethod("ManageGuideEmail", $.validator.methods.email, Messages["ManageGuideEmail"]);
        $.validator.addMethod("ManageGuideMaxLength15", $.validator.methods.maxlength, Messages["ManageGuideMaxLength15"]);
        $.validator.addMethod("ManageGuideMaxLength30", $.validator.methods.maxlength, Messages["ManageGuideMaxLength30"]);
        $.validator.addMethod("ManageGuideMaxLength50", $.validator.methods.maxlength, Messages["ManageGuideMaxLength50"]);
        $.validator.addMethod("ManageGuideMaxLength100", $.validator.methods.maxlength, Messages["ManageGuideMaxLength100"]);
        $.validator.addClassRules({
            Required: {
                ManageGuideRequireds: true,
            },
            Email: {
                ManageGuideEmail: true,
            },
            Date: {
                ManageGuideDate: true,
            },
            MaxLength100: {
                ManageGuideMaxLength100: 100,
            },
            MaxLength50: {
                ManageGuideMaxLength50: 50,
            },
            MaxLength30: {
                ManageGuideMaxLength30: 30,
            },
            MaxLength15: {
                ManageGuideMaxLength15: 15,
            },
        });

        $('#LoadGuideCompanyInfoForm').validate();
        $('#LoadGuidePersonalInfoForm').validate();
        LoadGuideGrid();
        $(".Tab").click(function (event) {

            if ($(this).hasClass("active")) {
                return false;
            }
            if ($(this).hasClass("Tab")) {
                CurrentTabElement = this;
            }
            if ((CurrentTab == 1 && !($('#GuideCompanyInfoSaveBtn').hasClass('BackColorRed'))) || (CurrentTab == 2 && !($('#GuidePersonalInfoSaveBtn').hasClass('BackColorRed'))) || CurrentTab == 3) {
                switch (CurrentTab) {
                    case 1:
                        {
                            $("#GuideCompanyInfo").removeClass("active");
                            $("#tabGuideCompanyInfo").removeClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    case 2:
                        {
                            $("#GuidePersonalInfo").removeClass("active");
                            $("#tabGuidePersonalInfo").removeClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    case 3:
                        {
                            $("#GuideAssignmentsForHidden").removeClass("active");
                            $("#tabGuideAssignments").removeClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                switch ($(CurrentTabElement).attr("id")) {
                    case "GuideCompanyInfo":
                        {
                            CurrentTab = 1;
                            $("#GuideCompanyInfo").addClass("active");
                            $("#tabGuideCompanyInfo").addClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    case "GuidePersonalInfo":
                        {
                            CurrentTab = 2;
                            $("#GuidePersonalInfo").addClass("active");
                            $("#tabGuidePersonalInfo").addClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    case "GuideAssignmentsForHidden":
                        {
                            CurrentTab = 3;
                            $("#GuideAssignmentsForHidden").addClass("active");
                            $("#tabGuideAssignments").addClass("active in");
                            GuideValidationClientSideHide = false;
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
            event.preventDefault();
            event.stopImmediatePropagation();
            if ($('#GuideCompanyInfoSaveBtn').hasClass('BackColorRed') && CurrentTab == 1) {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#GuideCompanyInfo").removeClass("active");
                        $("#tabGuideCompanyInfo").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "GuidePersonalInfo":
                                {
                                    CurrentTab = 2;
                                    $("#GuidePersonalInfo").addClass("active");
                                    $("#tabGuidePersonalInfo").addClass("active in");
                                    GuideValidationClientSideHide = false;
                                }
                                break;
                            case "GuideAssignmentsForHidden":
                                {
                                    CurrentTab = 3;
                                    $("#GuideAssignmentsForHidden").addClass("active");
                                    $("#tabGuideAssignments").addClass("active in");
                                    GuideValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
            else {
                jConfirm(Messages["ChangeTab"], Messages["ChangeTabTitle"], function (retVal) {
                    if (retVal != true) {
                        event.stopImmediatePropagation();
                        event.preventDefault();
                    }
                    else {
                        $("#GuidePersonalInfo").removeClass("active");
                        $("#tabGuidePersonalInfo").removeClass("active in");
                        switch ($(CurrentTabElement).attr("id")) {
                            case "GuideCompanyInfo":
                                {
                                    CurrentTab = 1;
                                    $("#GuideCompanyInfo").addClass("active");
                                    $("#tabGuideCompanyInfo").addClass("active in");
                                    GuideValidationClientSideHide = false;
                                }
                                break;
                            case "GuideAssignmentsForHidden":
                                {
                                    CurrentTab = 3;
                                    $("#GuideAssignmentsForHidden").addClass("active");
                                    $("#tabGuideAssignments").addClass("active in");
                                    GuideValidationClientSideHide = false;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
        });
    }
});
$(document).on('change', '.GuideCompanyElementChange', function () {
    if (!$('#GuideCompanyInfoSaveBtn').hasClass("disabled")) {
        $('#GuideCompanyInfoSaveBtn').addClass('BackColorRed');
    }
});
$(document).on('change', '.GuidePersonalElementChange', function () {
    if (!$('#GuidePersonalInfoSaveBtn').hasClass("disabled")) {
        $('#GuidePersonalInfoSaveBtn').addClass('BackColorRed');
    }
});
function ShowGuide(GuideID) {
    CurrentTab = 1;
    $("#GuideCompanyInfo").addClass("active");
    $("#tabGuideCompanyInfo").addClass("active in");
    GuideIDView = GuideID;
    if (GuideIDView == 0)
        AssignmentsHide();
    else
        AssignmentsShow();
    ShowGuideCompanyInfo(GuideIDView);
    ShowGuidePersonalInfo(GuideIDView);
    ShowGuideAssignments(GuideIDView);
}

function ShowGuideAssignments(GuideID) {
    $.ajax({
        url: urls["GuideLoadGuideAssignments"],
        data: { 'GuideID': GuideID },
        type: 'GET',
        success: function (data) {
            $("#tabGuideAssignments").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            ShowGuideDetailPopUp("GuideDetailsDiv");
            InitGuideDetailsDataCalendars();
        }
    });
}
function ShowGuideCompanyInfo(GuideID) {

    $.ajax({
        url: urls["GuideLoadGuideCompanyInfo"],
        data: { 'GuideID': GuideID },
        type: 'GET',
        success: function (data) {
            $("#tabGuideCompanyInfo").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            ShowGuideDetailPopUp("GuideDetailsDiv");
            InitGuideDetailsDataCalendars();
        }
    });
}
function ShowGuidePersonalInfo(GuideID) {

    $.ajax({
        url: urls["GuideLoadGuidePersonalInfo"],
        data: { 'GuideID': GuideID },
        type: 'GET',
        success: function (data) {
            $("#tabGuidePersonalInfo").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            ShowGuideDetailPopUp("GuideDetailsDiv");
            InitGuideDetailsDataCalendars();
        }
    });
}
function AssignmentsHide() {
    $("#GuideAssignmentsForHidden").hide();
}
function AssignmentsShow() {
    $("#GuideAssignmentsForHidden").show();
}
$(document).on('click', '#GuideMoreBtn', function () {
    PageNumber = PageNumber + 1;
    LoadGuideGrid();
});
$(document).on('click', '#GuideSearchBtn', function () {
    IsSearchView = true;
    LoadGuideGrid();
});
function ShowGuideDetailPopUp(PopUpDiv) {
    $.colorbox({
        width: "1000px",
        height: "450px",
        inline: true,
        href: "#" + PopUpDiv,
        onComplete: function () {
            SelectedLCountryID = $("#SelectedLCountryID").val();
            Phone = $("#Phone").val();
            Fax = $("#Fax").val();
            EmrgcyContactPhone = $("#EmrgcyContactPhone").val();
            FormattedPhoneNumbers(SelectedLCountryID);
            $(".phone").mask("(###) ###-####");
            $(".phoneUSACAN").mask("+1 (###) ###-####");
            $(".phoneTR").mask("+90 (###) ###-####");
            $(".phoneFR").mask("+33 (#) ##-##-##-##");
            $(".phoneBEL").mask("+32 (#) ###-##-##");
            $(".phoneSWI").mask("+41 (##) ###-####");
            $(".phoneTS").mask("+216 (##) ###-###");
            $(".phoneALG").mask("+213 (##) ###-####");
            $(".phoneMA").mask("+212 (##) ###-####");
            $(".SSNumber").mask("###-##-####");
            $(".PostalCode").mask("#####-####");
            $(".money").maskMoney({ prefix: '$ ', allowNegative: false, thousands: ',', decimal: '.', affixesStay: false });
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $("#" + PopUpDiv).hide();
        }
    });
}

$(document).on('click', '#GuideCompanyInfoSaveBtn', function () {
    var $form = $('#LoadGuideCompanyInfoForm');
    if ($form.valid()) {
        GuideValidationClientSideHide = false;
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: $form.serialize(),
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            },
            success: function (data) {
               if (GuideIDView == 0) 
                    $("#tabGuideCompanyInfo").html(data);
                
            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#GuideCompanyInfoSaveBtn').removeClass('BackColorRed')
                jAlert("Data SuccessFully Saved:", "Successful");
                ShowGuideCompanyInfo($('#CompanyInfoGuideID').val());
                ShowGuidePersonalInfo($('#CompanyInfoGuideID').val());
            }
        });
    }
    else
        GuideValidationClientSideHide = true;
});
$(document).on('click', '#GuidePersonalInfoSaveBtn', function () {
    var $form = $('#LoadGuidePersonalInfoForm');
    if ($form.valid()) {
        GuideValidationClientSideHide = false;
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: $form.serialize(),
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            },
            success: function (data) {
                if (GuideIDView == 0) {
                    $("#tabGuidePersonalInfo").html(data);
                }
            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#GuidePersonalInfoSaveBtn').removeClass('BackColorRed')
                jAlert("Data SuccessFully Saved:", "Successful");
                ShowGuidePersonalInfo($('#PersonalInfoGuideID').val());
                ShowGuideCompanyInfo($('#PersonalInfoGuideID').val());
            }
        });
    }
    else
        GuideValidationClientSideHide = true;
});
function InitGuideDetailsDataCalendars() {
    $(".GuideDeteilsDataCalendar").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: 'dd/M/yy',
        beforeShow: function (input) {
            $(input).css({
                "position": "relative",
                "z-index": 99999
            });
        }
    });
}
function LoadGuideGrid() {
    var ManageGuidesGridViewModel = {
        GuideNameText: $("#GuideNameText").val(),
        GuideLastNameText: $("#GuideLastNameText").val(),
        CitySearchText: $("#CitySearchText").val(),
        PageNum: parseInt(PageNumber),
        IsActiveSearch: $("#IsActiveSearch").val(),
        IsIsFindSearch: $("#IsIsFindSearch").val(),
        IsSearch: IsSearchView,
    };

    var DTO = JSON.stringify(ManageGuidesGridViewModel);
    $.ajax({
        url: urls["GuideLoadGuidesGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#ManageGuideGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {

        }
    });
}
$(document).on('change', '.CountryDrop', function () {
    FormattedPhoneNumbers($(this).val());
    if ($(this).val() === "") {
        $("#StateDiv").hide();
        $("#CityDiv").hide();
        $("#StateDivChange").show();
        $("#CityDivChange").show();
    }
    if ($(this).val() === '1') {
        $("#CityDiv").hide();
        $("#CityDivChange").show();
    }
    if ($(this).val() === '1') {
        $.ajax({
            url: urls["GuideGetStateByCountryID"],
            data: { CountryID: parseInt($(this).val()) },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.StateDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").show();
                $("#StateDivChange").hide();
            }
        });
    }
    else if ($(this).val() != "") {
        $.ajax({
            url: urls["GuideGetCityByCountryID"],
            data: { CountryID: parseInt($(this).val()), StateID: '0' },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("Select.CityDrop").html(markup);
            },
            error: function (reponse) {
                jAlert(reponse, "Error");
            },
            complete: function () {
                $("#StateDiv").hide();
                $("#StateDivChange").show();
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        });
    }
});
$(document).on('change', '.StateDrop', function () {
    var StateID = $(this).val();
    $.ajax({
        url: urls["GuideGetCityByCountryID"],
        data: { CountryID: 0, StateID: StateID },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("Select.CityDrop").html(markup);
        },
        error: function (reponse) {
            jAlert(reponse, "Error");
        },
        complete: function () {
            if (StateID === "" || StateID === "0") {
                $("#CityDiv").hide();
                $("#CityDivChange").show();
            }
            else {
                $("#CityDiv").show();
                $("#CityDivChange").hide();
            }
        }
    });
});
$(document).on('change', '#Phone', function () {
    Phone = $(this).val();
});
$(document).on('change', '#Fax', function () {
    Fax = $(this).val();
});
$(document).on('change', '#EmrgcyContactPhone', function () {
    EmrgcyContactPhone = $(this).val();
});
$(document).on('change', '#PhoneNumberUnformatted', function () {
    if ($("#PhoneNumberUnformatted").prop('checked') == true) {
        $(".phone").unmask();
        $(".phoneUSACAN").unmask();
        $(".phoneTR").unmask();
        $(".phoneFR").unmask();
        $(".phoneBEL").unmask();
        $(".phoneSWI").unmask();
        $(".phoneTS").unmask();
        $(".phoneALG").unmask();
        $(".phoneMA").unmask();

        $("#Phone").addClass("number");
        $("#Fax").addClass("number");
        $("#EmrgcyContactPhone").addClass("number");
        $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });

        $("#Phone").val(Phone);
        $("#Fax").val(Fax);
        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);
    }
    else {
        $(".number").unmask();
        $("#Phone").removeClass("number");
        $("#Fax").removeClass("number");
        $("#EmrgcyContactPhone").removeClass("number");
        FormattedPhoneNumbers($("#SelectedLCountryID").val())
    }
});

function FormattedPhoneNumbers(CountryID) {
    if ($("#PhoneNumberUnformatted").prop('checked') != true) {
        $("#Phone").val(Phone);
        $("#Fax").val(Fax);
        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);

        switch (CountryID) {
            case '1': case '7':
                {
                    if (SelectedLCountryID == '1' || SelectedLCountryID == '7') {
                        $("#Phone").val(Phone);
                        $("#Fax").val(Fax);
                        $("#EmrgcyContactPhone").val(EmrgcyContactPhone);
                    }
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneUSACAN');
                    $(".phoneUSACAN").mask("+1 (###) ###-####");
                };
                break;
            case '3':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTR');
                    $(".phoneTR").mask("+90 (###) ###-####");
                };
                break;
            case '5':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneFR');
                    $(".phoneFR").mask("+33 (#) ##-##-##-##");
                };
                break
            case '8':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneBEL');
                    $(".phoneBEL").mask("+32 (#) ###-##-##");
                };
                break
            case '11':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneSWI');
                    $(".phoneSWI").mask("+32 (#) ###-##-##");
                };
                break
            case '12':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneTS');
                    $(".phoneTS").mask("+216 (##) ###-###");
                };
                break
            case '24':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneALG');
                    $(".phoneALG").mask("+213 (##) ###-####");
                };
                break
            case '26':
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phoneMA');
                    $(".phoneMA").mask("+212 (##) ###-####");
                };
                break
            default:
                {
                    $("#Phone").removeAttr('class');
                    $("#Phone").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#Fax").removeAttr('class');
                    $("#Fax").addClass('GuideCompanyElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $("#EmrgcyContactPhone").removeAttr('class');
                    $("#EmrgcyContactPhone").addClass('GuidePersonalElementChange table_inputs_SmallPadding2 inpputgr form-control phone');
                    $(".phone").mask("(###) ###-####");
                };
        }
    }
}

// text element complete text 
$(document).on('input', '.QuotaTxtComplete', function () {
    if (GuideTimerId == null) {
        GuideAutoComElem = $(this).attr('id');
        GuideAutoComElemVal = $("#" + GuideAutoComElem).val();
        GuideTimerId = setInterval(function () {

            var keyword = $("#" + GuideAutoComElem).val();
            if (keyword == GuideAutoComElemVal) {
                var PropName = "";
                if (GuideAutoComElem.indexOf("GuideNameText") != -1)
                    PropName = "GuideNameText";
                if (GuideAutoComElem.indexOf("GuideLastNameText") != -1)
                    PropName = "GuideLastNameText";

                var position = $("#" + GuideAutoComElem).offset();
                position.top = position.top + $("#" + GuideAutoComElem).height() + 5;
                $.ajax({
                    url: urls["GuideAutoComplete"],
                    data: { 'PropName': PropName, 'keyword': keyword },
                    type: 'POST',
                    success: function (data) {
                        $("#AutoCompleteDiv").html(data);
                        var width = $("#" + GuideAutoComElem).css("width");
                        $("#AutoCompleteDiv").css("width", width);
                        $("#AutoCompleteDiv").show();
                        $("#AutoCompleteDiv").offset(position);
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        clearInterval(GuideTimerId);
                        GuideTimerId = null;
                    }
                });
            }
            else
                GuideAutoComElemVal = keyword;
        }, 600);
    }
});
$(document).on('click', '.QuotaAutoCompleteDivClose', function () {
    $(this).parent().parent().hide();
});
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function AutoCompleteRowClick(key, val) {
    val = htmlDecode(val);
    $("#" + GuideAutoComElem).val(val);
    if (GuideAutoComElem.indexOf("GuideNameText") != -1)
        $("#" + GuideAutoComElem).next().val(key);
    if (GuideAutoComElem.indexOf("GuideLastNameText") != -1)
        $("#" + GuideAutoComElem).next().val(key);
    $("#AutoCompleteDiv").hide();
}
var originalClose = $.colorbox.close;
$.colorbox.close = function () {
    if ($('#GuideCompanyInfoSaveBtn').hasClass('BackColorRed') || $('#GuidePersonalInfoSaveBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ClosePopUp"], Messages["ClosePopUpTitle"], function (retVal) {
            if (retVal == true) {
                $("#tabGuidePersonalInfo").empty();
                $("#tabGuideCompanyInfo").empty();
                $("#GuideAssignmentsForHidden").empty();
                $("#tabGuideCompanyInfo").removeClass("active in");
                $("#tabGuidePersonalInfo").removeClass("active in");
                $("#tabGuideAssignments").removeClass("active in");
                $("#GuideCompanyInfo").removeClass("active");
                $("#GuidePersonalInfo").removeClass("active");
                $("#GuideAssignmentsForHidden").removeClass("active");
                LoadGuideGrid();
                originalClose();
            }
        });
    }
    else {
        $("#tabGuidePersonalInfo").empty();
        $("#tabGuideCompanyInfo").empty();
        $("#GuideAssignmentsForHidden").empty();
        $("#tabGuideCompanyInfo").removeClass("active in");
        $("#tabGuidePersonalInfo").removeClass("active in");
        $("#tabGuideAssignments").removeClass("active in");
        $("#GuideCompanyInfo").removeClass("active");
        $("#GuidePersonalInfo").removeClass("active");
        $("#GuideAssignmentsForHidden").removeClass("active");
        LoadGuideGrid();
        originalClose();
    }
};
$(document).on('change', '.ElementsForValidation', function () {
    if ($(this).valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
    if (GuideValidationClientSideHide) {
        if ($(this).closest("form").valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
    }
    else if ($(this).valid()) {
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            $(this).valid();
        });
    }
});
